<?php

namespace app\admin\service\diy;

use app\api\service\ArticleService;
use app\common\model\diy\Diy;
use tmcore\base\BaseService;

class DiyService extends BaseService
{

    /**
     * 查看diy数据
     * @param $params
     * @return array
     */
    public static function detail($params) : array
    {
        $row = Diy::where(['name' => $params['name'], 'type' => $params['type']])->findOrEmpty()->toArray();

        if($row && $params['type'] == 1) {
            $info = [];
            $cache = [];

            // 获取被选中的移动端样式
            if($params['name'] == 'home_page' || $params['name'] == 'person_center') {
                $cache = (new Diy)->getDiyCache(1, 'style');
            }

            switch ($params['name'])
            {
                case 'home_page':
                    $info = [
                        'article_list' => ArticleService::getHomeList([]),
                        'style_identify' => !empty($cache['identify'])?$cache['identify']:'blue',
                        'style_color' => !empty($cache['color'])?$cache['color']:'#2979ff'
                    ];
                    break;
                case 'person_center':
                    $info = [
                        'user_avatar' => 'resource/admin/images/person_avatar.png',
                        'person_center_top' => 'resource/admin/images/person_center_top.png',
                        'style_identify' => !empty($cache['identify'])?$cache['identify']:'blue',
                        'style_color' => !empty($cache['color'])?$cache['color']:'#2979ff'
                    ];
                    break;
                case 'style':
                    $info = [
                        ['name' => '天空蓝', 'color' => '#508AFF', 'identify' => 'blue', 'image_url' => 'resource/admin/images/blue_style.png'],
                        ['name' => '生鲜绿', 'color' => '#19F347', 'identify' => 'green', 'image_url' => 'resource/admin/images/green_style.png'],
                        ['name' => '热情红', 'color' => '#D4237A', 'identify' => 'red', 'image_url' => 'resource/admin/images/red_style.png'],
                    ];
                    break;
                default:
            }
            $row['info'] = $info;
        }

        return $row;
    }

    /**
     * 编辑diy数据
     * @param array $params
     * @return bool
     */
    public static function edit(array $params) : bool
    {
        $res = Diy::where(['name' => $params['name'], 'type' => $params['type']])->update(['data' => json_encode($params['data'], 320)]);
        if(false !== $res) {
            (new Diy)->setDiyCache($params['type'], $params['name'], $params['data']);
            return true;
        } else {
            return false;
        }
    }
}